package impl;

import amushen.base.BaseBO;
import vo.*;

import java.util.*;
import utils.*;

public class TreeBO extends BaseBO{

	/*
	 * public String getTreeNode(String id){		
		String rst="{";
		boolean isProduct=false;		
		if(id==null||id.length()<1||id.equals("-1"))
			return getRoot();
		if(id.charAt(0)=='p'){
			isProduct=true;		
		}
		
		id=id.substring(2);
		getSession();
		try{
		if(isProduct){
			Product pro=(Product)sess.get(Product.class , new Integer(id));
			rst+="objectId:'p_"+id+"',name:'"+pro.getName()+"',type:'3',proType:'"+pro.getProType()+"'";
		}
		else{
			ProductCate pc=(ProductCate)sess.get(ProductCate.class, new Integer(id));
			rst+="objectId:'c_"+id+"',name:'"+pc.getCateName()+"',type:'2'";
		}
		List c1,c2;
		if(!isProduct){
			c1=getCategoryChildren(new Integer(id));
			c2=getProductChildren(new Integer(id));
			if(c1.size()+c2.size()>0){
				rst+=",children:";
				if(c1.size()+c2.size()>1)
					rst+="[";
				for(int j=0;j<c1.size();j++){
					Object[] o1=(Object[])c1.get(j);
					rst+="{stub:'c_"+o1[0]+"'},";
				}
				for(int j=0;j<c2.size();j++){
					Object[] o1=(Object[])c2.get(j);
					rst+="{stub:'p_"+o1[0]+"'},";
				}
				rst=rst.substring(0,rst.length()-1);	//remove last comma
				if(c1.size()+c2.size()>1)
					rst+="]";
				rst+="}\n";
			}else{
				rst+="}\n";
			}
		}else{
			rst+="}\n";
		}
		commit();
		}catch(Exception e){rollback();return "";};
		return rst;
	}
	
		
	 public String getRoot(){
		String cate="{label:'name',identifier: 'objectId',items: [\n";
		getSession();
		List list=sess.createSQLQuery("select id,cate_name from tbl_product_cate where parent = 1").list();
		for(int i=0;i<list.size();i++){
			Object[] obj=(Object[])list.get(i);
			cate+="{ objectId :'c_"+obj[0]+"',name:'"+obj[1]+"',type:'root'";
			List c1,c2;
			c1=getCategoryChildren((Integer)obj[0]);
			c2=getProductChildren((Integer)obj[0]);
			if(c1.size()+c2.size()>0){
				cate+=",children:[";
				for(int j=0;j<c1.size();j++){
					Object[] o1=(Object[])c1.get(j);
					cate+="{_reference:'c_"+o1[0]+"'},";
				}
				for(int j=0;j<c2.size();j++){
					Object[] o1=(Object[])c2.get(j);
					cate+="{_reference:'p_"+o1[0]+"'},";
				}
				cate=cate.substring(0,cate.length()-1);	//remove last comma
				cate+="]},\n";
				for(int j=0;j<c1.size();j++){
					Object[] o1=(Object[])c1.get(j);
					cate+="{objectId:'c_"+o1[0]+"',name:'"+o1[1]+"',type:'stub'},";
				}
				for(int j=0;j<c2.size();j++){
					Object[] o1=(Object[])c2.get(j);
					cate+="{objectId:'p_"+o1[0]+"',name:'"+o1[1]+"',type:'3',proType:'"+o1[2]+"'},";
				}
//				cate=cate.substring(0,cate.length()-1);	//remove last comma				
			}
			else{
				cate+="},";
			}			
		}
		cate=cate.substring(0,cate.length()-1);	//remove last comma		
		commit();
		return cate+"]}";
	}
	 */
	 public String getRoot(){
			String cate="{label:'name',identifier: 'objectId',items: [\n";
			getSession();
			List list=sess.createSQLQuery("select id,cate_name from tbl_product_cate where parent = 1").list();
			for(int i=0;i<list.size();i++){
				Object[] obj=(Object[])list.get(i);
				cate+="{ objectId :'c_"+obj[0]+"',name:'"+obj[1]+"',type:'root'";
				List c1;
				c1=getCategoryChildren((Integer)obj[0]);
				if(c1.size()>0){
					cate+=",children:[";
					for(int j=0;j<c1.size();j++){
						Object[] o1=(Object[])c1.get(j);
						cate+="{_reference:'c_"+o1[0]+"'},";
					}					
					cate=cate.substring(0,cate.length()-1);	//remove last comma
					cate+="]},\n";
					for(int j=0;j<c1.size();j++){
						Object[] o1=(Object[])c1.get(j);
						cate+="{objectId:'c_"+o1[0]+"',name:'"+o1[1]+"',type:'stub'},";
					}					
//					cate=cate.substring(0,cate.length()-1);	//remove last comma				
				}
				else{
					cate+=",leaf:'1'},";
				}			
			}
			cate=cate.substring(0,cate.length()-1);	//remove last comma		
			commit();
			return cate+"]}";
		}
	 
	 public String getTreeNode(String id){		
			String rst="{";
			boolean isProduct=false;		
			if(id==null||id.length()<1||id.equals("-1"))
				return getRoot();
			if(id.charAt(0)=='p'){
				isProduct=true;		
			}
			
			id=id.substring(2);
			getSession();
			try{
			if(isProduct){
				Product pro=(Product)sess.get(Product.class , new Integer(id));
				rst+="objectId:'p_"+id+"',name:'"+pro.getName()+"',type:'3',proType:'"+pro.getProType()+"'";
			}
			else{
				ProductCate pc=(ProductCate)sess.get(ProductCate.class, new Integer(id));
				rst+="objectId:'c_"+id+"',name:'"+pc.getCateName()+"',type:'2'";
			}
			List c1,c2;
			if(!isProduct){
				c1=getCategoryChildren(new Integer(id));
				c2=getProductChildren(new Integer(id));
				if(c1.size()+c2.size()>0){
					rst+=",children:";
					if(c1.size()+c2.size()>1)
						rst+="[";
					for(int j=0;j<c1.size();j++){
						Object[] o1=(Object[])c1.get(j);
						rst+="{stub:'c_"+o1[0]+"'},";
					}
					for(int j=0;j<c2.size();j++){
						Object[] o1=(Object[])c2.get(j);
						rst+="{stub:'p_"+o1[0]+"'},";
					}
					rst=rst.substring(0,rst.length()-1);	//remove last comma
					if(c1.size()+c2.size()>1)
						rst+="]";
					rst+="}\n";
				}else{
					rst+="}\n";
				}
			}else{
				rst+="}\n";
			}
			commit();
			}catch(Exception e){rollback();return "";};
			return rst;
		}
	public List getCategoryChildren(Integer parentId){		
		List list=new ArrayList();
		list=sess.createSQLQuery("select id,cate_name from tbl_product_cate where parent = "+parentId).list();
		return list;
	}
	
	public List getProductChildren(Integer parentId){
		List list=new ArrayList();
		list=sess.createSQLQuery("select id,name,pro_type from tbl_product where cate_id="+parentId).list();
		return list;
	}
}
